package demo2;

import java.util.Comparator;

class Person {
    public String name;
    public int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class AgeCom implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        //原始语句
        //return 0;
        return o1.age - o2.age;
    }
}
class NameCom implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    public static void main(String[] args) {
        Person person = new Person("小王", 20);
        Person person1 = new Person("小李", 18);
        AgeCom ageCom = new AgeCom();
        System.out.println(ageCom.compare(person, person1));
        NameCom nameCom = new NameCom();
        System.out.println(nameCom.compare(person, person1));
    }
}





//class Person implements Comparable<Person> {
//    public String name;
//    public int age;
//
//    public Person(String name, int age) {
//        this.name = name;
//        this.age = age;
//    }
//
//    @Override
//    public String toString() {
//        return "Person{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
//    @Override
//    public int compareTo(Person o) {
//        return this.age - o.age;
//    }
//
////    @Override
////    public int compareTo(Person o) {
////        //原始语句
////        //return 0;
//////        if (this.age > o.age) {
//////            return 1;
//////        } else if (this.age < o.age) {
//////            return -1;
//////        } else {
//////            return 0;
//////        }
////        return this.age - o.age;
////    }
//}
//
//public class Test {
//    public static void main(String[] args) {
//        Person person = new Person("小王", 20);
//        Person person1 = new Person("小李", 18);
//        System.out.println(person.compareTo(person1));
//    }
//}
